package com.xjt.wx_admin.entity;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.xjt.wx_admin.util.GenerateNickName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * <p>
 *
 * </p>
 *
 * @author Xujt
 * @since 2023-03-22 11:33:28
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 用户名
     */
    private String userName;

    /**
     * 密码
     */
    private String password;

    /**
     * 昵称
     */
    private String name;

    /**
     * 在线状态 1：在线  0：离线
     */
    private Integer online;

    /**
     * 头像
     */
    private String avatar;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;


    /**
     * 好友id，如果已存在则代表已是好友
     */
    @TableField(exist = false)
    Long friendId;

    /**
     * 是否存在申请记录
     */
    @TableField(exist = false)
    Long applyId;

    /**
     * 是否存在被接受记录
     */
    @TableField(exist = false)
    Long userId;

    /**
     * 好友备注
     */
    @TableField(exist = false)
    String remark;

    public User(String userName){
        this.userName = userName;
        this.online = 0;
//        获取随机头像
        String result = HttpUtil.get("https://api.vvhan.com/api/avatar?type=json");
        JSONObject jsonObject = JSONObject.parseObject(result);
        this.avatar = jsonObject.getString("avatar");
//        获取随机名称
        this.name = GenerateNickName.gen(3);
//        得到随机密码
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        this.password = encoder.encode(RandomUtil.randomNumbers(6));
        this.createTime = LocalDateTime.now();
    }

    public User(String userName,String password){
        this.userName = userName;
        String result = HttpUtil.get("https://api.vvhan.com/api/avatar?type=json");
        JSONObject jsonObject = JSONObject.parseObject(result);
        this.avatar = jsonObject.getString("avatar");
//        获取随机名称
        this.name = GenerateNickName.gen(3);
        this.online = 0;
//        得到随机密码
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        this.password = encoder.encode(password);
        this.createTime = LocalDateTime.now();
    }
}
